from pyspark import SparkConf, SparkContext

if __name__ == '__main__':
    # 构建SparkConf对象
    conf = SparkConf().setAppName("test").setMaster("local[*]")
    # 构建SparkContext执行环境入口对象
    sc = SparkContext(conf=conf)

    rdd = sc.parallelize([("a", 1), ("a", 2), ("b", 3), ("b", 10)])

    rdd = rdd.groupByKey()
    """
    groupByKey跟groupBy类似，
    groupByKey只针对KV型数据，且分组条件默认是key,
    而groupBy需要自定义函数指定分组的key
    """

    rdd = rdd.map(lambda t: (t[0], list(t[1])))
    """
    结果为：[('b', [3, 10]), ('a', [1, 2])]
    
    groupByKey分组后结果只保留了value
    而groupBy分组后保留了元素本身
    """
    print(rdd.collect())
